<?php

class Simq {

    static private $_server = array (
            'host' => '192.168.30.59',
            'port' => 27777,
            'auth' => 'mien12345'
        );

    // 推送消息
    static public function put($queue, $msg) {
         
        // 生成uri
        $uri = sprintf('/%s', $queue);

        // 推送到simq
        $r = self::_httpSocket('PUT', $uri, $msg);
    
        // 返回结果
        return ($r !== false) && ($r['Count'] > 0);
    }

    // 获取消息
    static public function get($queue) {
         
        // 生成uri
        $uri = sprintf('/%s', $queue);

        // 推送到simq
        $r = self::_httpSocket('GET', $uri);
    
        // 返回结果
        if ($r === false)
            return null;
        return $r['Body'];
    }

    // 获取队列长度
    static public function len($queue) {
         
        // 生成uri
        $uri = sprintf('/%s', $queue);

        // 推送到simq
        $r = self::_httpSocket('HEAD', $uri);
    
        // 返回结果
        if ($r === false)
            return 0;
        return $r['Count'];
    }

    // 订阅
    static public function sub($queue, $url) {
         
        // 生成uri
        $uri = sprintf('/%s', $queue);

        // 推送到simq
        $r = self::_httpSocket('SUB', $uri, $url);
    
        // 返回结果
        return ($r !== false) && ($r['Count'] > 0);
    }

    // 退订
    static public function unsub($queue, $url) {
         
        // 生成uri
        $uri = sprintf('/%s', $queue);

        // 推送到simq
        $r = self::_httpSocket('UNSUB', $uri, $url);
    
        // 返回结果
        return ($r !== false) && ($r['Count'] > 0);
    }
     
    // 连接 simq 服务
    static private function _httpSocket($method, $uri, $data = null, $timeout = 30) {
        
        $fsock = fsockopen (self::$_server['host'], self::$_server['port'], $errno, $errstr, $timeout);
        if (! $fsock) {
            return false;
        }
        
        $method = strtoupper($method);
        $in = $method . ' ' . $uri . " HTTP/1.1\r\n";
        $in .= 'Auth: ' . self::$_server['auth'] . "\r\n";
        if ($data) {
            $in .= "Content-Length: " . strlen ( $data ) . "\r\n\r\n";
            $in .= $data;
        } else {
            $in .= "\r\n";
        }
        
        if (! @fwrite ( $fsock, $in, strlen ( $in ) )) {
            fclose ( $fsock );
            return false;
        }
        
        $out = null;
        while ( $buff = @fgets ( $fsock, 2048 ) ) {
            $out .= $buff;
        }
        fclose ( $fsock );
        
        $arr = array();

        $pos1 = strpos ($out, "\r\n");
        $firstline = substr ( $out, 0, $pos1 );
        if (preg_match ( "/^HTTP\/\d\.\d\s(\d+)\s.*$/", $firstline, $matches )) {
            $arr['Status'] = $matches[1];
        }

        $pos1 += 2;
        $pos2 = strpos ( $out, "\r\n\r\n", $pos1 );

        $headers = substr ( $out, $pos1, $pos2 - $pos1 );
        $arr['Body'] = substr ( $out, $pos2 + 4 );

        $headers = trim($headers);
        if (!empty($headers)) {
            $lines = explode("\r\n", $headers);
            foreach ($lines as $line) {
                if (empty($line)) continue;
                list($key, $value) = explode(':', $line);
                $arr[$key] = trim($value);
            }
        }

        return $arr;
    }
}
